{% macro pager(page, pages, generator) -%}
    {% if pages > 1 %}
        <div class="pagination">
            <ul>
                {% set min = page - 3 %}
                {% set max = page + 3 + 1 %}

                {% if min < 0 %}
                    {% set max = max - min %}
                {% endif %}
                {% if max >= pages %}
                    {% set min = min - max + pages %}
                {% endif %}

                {% if min < 0 %}
                    {% set min = 0 %}
                {% endif %}
                {% if max >= pages %}
                    {% set max = pages %}
                {% endif %}

                {% if min > 0 %}
                    <li>
                        <a href="{{ generator(0) }}">&laquo;</a>
                    </li>
                {% else %}
                    <li class="disabled">
                        <a href="#">&laquo;</a>
                    </li>
                {% endif %}
                {% if page > 0 %}
                    <li>
                        <a href="{{ generator(page-1) }}">&lt;</a>
                    </li>
                {% else %}
                    <li class="disabled">
                        <a href="#">&lt;</a>
                    </li>
                {% endif %}

                {% for p in range(min, max) %}
                    {% if page == p %}
                        <li class="active">
                            <a href="#">{{ p + 1 }}</a>
                        </li>
                    {% else %}
                        <li>
                            <a href="{{ generator(p) }}">{{ p + 1 }}</a>
                        </li>
                    {% endif %}
                {% endfor %}

                {% if page + 1 < pages %}
                    <li>
                        <a href="{{ generator(page + 1) }}">&gt;</a>
                    </li>
                {% else %}
                    <li class="disabled">
                        <a href="#">&gt;</a>
                    </li>
                {% endif %}
                {% if max < pages %}
                    <li>
                        <a href="{{ generator(pages - 1) }}">&raquo;</a>
                    </li>
                {% else %}
                    <li class="disabled">
                        <a href="#">&raquo;</a>
                    </li>
                {% endif %}
            </ul>
        </div>
    {% endif %}
{%- endmacro %}

{% macro render_field(field, show_error_list=True) %}
    <div {% if show_error_list and field.errors %}class="error"{% endif %}>
        {{ field.label }}
        {{ field(**kwargs)|safe }}
        {% if show_error_list and field.errors %}
            <ul class="errors_list">
                {% for error in field.errors %}<li>{{ error }}{% endfor %}
            </ul>
        {% endif %}
    </div>
{% endmacro %}

{% macro render_ff (ff, delete) %}
    <section class="field">
        {% if ff.type == "FormField" %}
      
            {% if delete %}
                <span class="delete">Delete</span>
            {% else %}
                <h3>{{ admin_view.field_name(ff.short_name) }}</h3>
            {% endif %}

            <div>{{ render_formfield(ff.form) }}</div>
        {% elif ff.type == "ListField" %}
            <h3>{{ admin_view.field_name(ff.short_name) }}</h3>
            {% if delete %}
                <span class="delete">Delete</span>
            {% endif %}
            {% set a = ff.new_generic() %}
            <div class="list_par">
                <div class="append-list" style="display:none;">{{ render_ff(a, True) }}</div>
                {% for field in ff %}
                    {{ render_ff(field, admin_view.can_edit or not instance) }}
                {% endfor %}
                {% if admin_view.can_edit or not instance %}
                    <button class="append btn">+</button>
                {% endif %}
                <div class="clear"></div>
            </div>
        {% else %}
            
            {% if delete %}
                <span class="delete">Delete</span>
            {% else %}
                <label for="{{ ff.label.field_id }}">{{ admin_view.field_name(ff.label.text) }}</label>
            {% endif %}

            <div>
                {% set class='' %}
                {% if ff.type == "DateTimeField" %}
                    {% set data_type="datetimepicker" %}
                {% endif %}

                {% if ff.errors|length>0 %} {% set class=class+' error' %}{% endif %}

                {% if not admin_view.can_edit and instance %}

                    {% with reference = admin_view.get_reference(ff.data) %}
                        {% if reference %}
                            <div class="readonly-value"><a href="{{ reference }}">{{ ff.data }}</a></div>
                        {% else %}
                            <div class="readonly-value">{{ ff.data }}</div>
                        {% endif %}
                    {% endwith %}

                {% else %}
                    {{ ff(class=class) }}
                {% endif %}
        
                {% for error in ff.errors %}
                    &nbsp;<span class="label label-important">{{ error }}</span>
                {% endfor %}

                {% if ff.description %}
                    <p class="help-block">{{ ff.description }}</p>
                {% endif %}
            </div>
        {% endif %}
   </section>
{% endmacro %}

{% macro render_formfield(form) %}
    <fieldset>
        {% set readonly_fields = admin_view.get_readonly_fields(instance) %}
        {% set field_names = admin_view.fields or form._fields.keys() %}
        
        {% for field_name in field_names %}
            {% if field_name in readonly_fields %}
                {% with f = readonly_fields[field_name] %}
                    <section class="field">
                        <label>{{ f.label }}</label>
                        <div>
                            <div class="readonly-value">
                                {% if f.url %}
                                    <a href="{{ f.url }}"{% if f.url.startswith('http') %}target="_blank"{% endif %}>{{ f.value }}</a>
                                {% else %}
                                    {{ f.value }}
                                {% endif %}
                            </div>
                        </div>
                    </section>
                {% endwith %}
            {% else %}
                {% if field_name != 'csrf_token' and field_name != 'csrf' %}
                    {{ render_ff(form._fields[field_name]) }}
                {% endif %}
            {% endif %}
        {% endfor %}

    </fieldset>
{% endmacro %}

{% macro render_form(form, extra=None, can_edit=True, can_delete=True) -%}
    <form action="#" method="POST"{% if form.has_file_field %} enctype="multipart/form-data"{% endif %}>
        {{ form.hidden_tag() if form.hidden_tag is defined }}

        {{ render_formfield(form) }}

        {# if the view is not editable nor deletable, there's no point to show any buttons #}
        {% if can_edit or can_delete %}
            <div class="form-buttons">
                {% if can_edit or not instance %}
                    <input type="submit" class="btn btn-primary" value="{{ _gettext('Submit') }}" />
                {% endif %}
                {% if extra %}
                    {{ extra }}
                {% endif %}
            </div>
        {% endif %}
    </form>
{% endmacro %}

